package com.laiketui.common.utils.map;

/**
 * 地图坐标转换器
 */
public class CoordinateConverter {
    private static final double PI = 3.1415926535897932384626;
    private static final double A = 6378245.0;
    private static final double EE = 0.00669342162296594323;

    /**
     * WGS-84 转 GCJ-02
     *
     * @param wgsLat WGS-84 纬度
     * @param wgsLon WGS-84 经度
     * @return GCJ-02 经纬度数组 [纬度, 经度]
     */
    public static double[] wgs84ToGcj02(double wgsLat, double wgsLon) {
        if (outOfChina(wgsLat, wgsLon)) {
            return new double[]{wgsLat, wgsLon};
        }
        double[] delta = delta(wgsLat, wgsLon);
        double gcjLat = wgsLat + delta[0];
        double gcjLon = wgsLon + delta[1];
        return new double[]{gcjLat, gcjLon};
    }

    /**
     * 判断坐标是否在中国以外
     *
     * @param lat 纬度
     * @param lon 经度
     * @return 是否在中国以外
     */
    private static boolean outOfChina(double lat, double lon) {
        return lon < 72.004 || lon > 137.8347 || lat < 0.8293 || lat > 55.8271;
    }

    /**
     * 计算 WGS-84 与 GCJ-02 的偏差
     *
     * @param lat 纬度
     * @param lon 经度
     * @return 偏差数组 [纬度偏差, 经度偏差]
     */
    private static double[] delta(double lat, double lon) {
        double dLat = transformLat(lon - 105.0, lat - 35.0);
        double dLon = transformLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * PI;
        double magic = Math.sin(radLat);
        magic = 1 - EE * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((A * (1 - EE)) / (magic * sqrtMagic) * PI);
        dLon = (dLon * 180.0) / (A / sqrtMagic * Math.cos(radLat) * PI);
        return new double[]{dLat, dLon};
    }

    /**
     * 纬度转换公式
     *
     * @param x 经度偏移量
     * @param y 纬度偏移量
     * @return 转换后的纬度
     */
    private static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    /**
     * 经度转换公式
     *
     * @param x 经度偏移量
     * @param y 纬度偏移量
     * @return 转换后的经度
     */
    private static double transformLon(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0;
        return ret;
    }

    public static void main(String[] args) {
        // 示例：微信地图拾取的 WGS-84 坐标（北京天安门）
        double wgsLat = 22.714627;
        double wgsLon = 114.30334;

        // 转换为 GCJ-02 坐标
        double[] gcj = wgs84ToGcj02(wgsLat, wgsLon);
        System.out.println("GCJ-02 Latitude: " + gcj[0] + ", Longitude: " + gcj[1]);
    }
}
